<?php 
namespace app\index\model;
use think\Model;
use think\Db;
class User extends Model
{
    public function login($username, $password){
        $user = Db::name('laytp_worker')
            ->field('
                id as uid, 
                user as uname, 
                pass as upass, 
                nickname as unik, 
                role as urole, 
                status as status
            ')
            ->where('user','=',$username)
            ->find();
        return $user;
    }

    public function get_workers($id=null, $page=1, $limit=5)
    {
        if($id != null){//
            $map["id"] = $id;//where _pid = $id
        }else{
            $map = "1=1 and id > 1";//where true
        }
        $user = Db::name('laytp_worker')
            ->field('
                user as account,
                nickname as unik, 
                role as urole,
                passstatus as pstatus,
                sex as sex,
                identity as identity,
                birthday as birthday,
                status as status
            ')
            ->where($map)
            ->page($page)
            ->limit($limit)
            ->select();
        $count = Db::name('laytp_worker')
            -> where($map)
            -> count();
        if($user){
            return array("status"=>1, "count"=>$count, "data"=>$user);
        }else{
            return array("status"=>0, "count"=>0, "data"=>[]);
        }
    }

    public function get_user_info($username)
    {
        $user = Db::name('laytp_worker')
            ->field('
                id as uid, 
                user as uname, 
                pass as upass, 
                nickname as unik, 
                role as urole, 
                status as status
            ')
            ->where('user','=',$username)
            ->find();
        return $user;
    }

    public function create_user($worker, $pass, $nik, $sex, $identity, $birthday, $role)
    {
        $res = Db::table("laytp_worker")
            ->insert(array(
                "user" => $worker,
                "pass" => $pass,
                "passstatus" => 0,
                "nickname" => $nik,
                "sex" => $sex,
                "identity" => $identity,
                "birthday" => $birthday,
                "role" => $role,
                "saleslevel" => 1,
                "status" => 1,
            ));
        return $res;
    }

    public function update_user($type, ...$params)
    {
        switch ($type) {
            case 'modify_pass':
                $user = $params[0];//id
                $newpass = $params[1];
                $res = Db::table('laytp_worker')
                    ->where('user', $user)
                    ->update(['pass'=>$newpass]);
                break;
            case 'worker_able':
                $user = $params[0];//user
                $status = $params[1];
                $res = Db::table('laytp_worker')
                    ->where('user', $user)
                    ->update(['status'=>$status]);
                break;
            case 'worker_repass':
                $user = $params[0];//id
                $newpass = $params[1];
                $res = Db::table('laytp_worker')
                    ->where('user', $user)
                    ->update(['pass'=>$newpass,'passstatus'=>0]);
                break;
            default:
                # code...
                break;
        }
        return $res;
    }
}
